﻿#include "widget.h"

#include <QHBoxLayout>
#include <QLabel>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    qDebug()<<"initUI begin!";
    initialUI();
    //initUdp();

}

Widget::~Widget()
{
}

void Widget::updateRole(int index)
{
    sendFlag = index == 0 ? true : false;
    hostPort = listenPort;
    listenPort = sendFlag ? listenPort -1 : listenPort + 1;
    portHostLineEdit->setText(QString::number(hostPort));
    portListenLineEdit->setText(QString::number(listenPort));
    qDebug()<<"updateRole";
}

void Widget::updateIp()
{
    hostIp = ipLineEdit->text();
    qDebug()<<"updateIp";
}

void Widget::updatePort()
{
    qDebug()<<"updatePort";
    listenPort = portListenLineEdit->text().toInt();
    if(sendFlag){//send
        if( listenPort % 2 == 1)//odd
        {
            listenPort += 1;
        }
        hostPort = listenPort + 1;
    } else {//recv
        if( listenPort % 2 == 0)//even
        {
            listenPort += 1;
        }
        hostPort = listenPort -1;
    }

    portListenLineEdit->setText(QString::number(listenPort));
    portHostLineEdit->setText(QString::number(hostPort));
    qDebug()<<"updatePort";
}

void Widget::initialUI()
{
    QHBoxLayout* hLayout1 = new QHBoxLayout();
    QHBoxLayout* hLayout2 = new QHBoxLayout();
    QHBoxLayout* hLayout3 = new QHBoxLayout();
    QHBoxLayout* hLayout4 = new QHBoxLayout();
    QHBoxLayout* hLayout5 = new QHBoxLayout();


    QVBoxLayout* vLayout1 = new QVBoxLayout(this);

    QLabel *roleLabel = new QLabel("角   色");
    QLabel *ipLabel =   new QLabel("对端地址");
    QLabel *portHostLabel = new QLabel("对端端口");
    QLabel *portListenLabel = new QLabel("本端端口");

    roleBox = new QComboBox();
    roleBox->addItem("发送端");
    roleBox->addItem("接收端");
    roleBox->setCurrentIndex(0);
    connect(roleBox,SIGNAL(currentIndexChanged(int)),
            this,SLOT(updateRole(int)));


    ipLineEdit = new QLineEdit(hostIp);
    ipLineEdit->setInputMask("000.000.000.000");
    ipLineEdit->setAlignment(Qt::AlignRight);
    connect(ipLineEdit,SIGNAL(editingFinished()),
            this,SLOT(updateIp()));

    portHostLineEdit = new QLineEdit(QString::number(hostPort));
    portHostLineEdit->setInputMask("00000");
    portHostLineEdit->setAlignment(Qt::AlignRight);
    //portLineEdit->setReadOnly(true);
    portHostLineEdit->setEnabled(false);
    //connect(portHostLineEdit,SIGNAL(textChanged(QString)),
    //        this,SLOT(updatePort(QString)));

    portListenLineEdit = new QLineEdit(QString::number(listenPort));
    portListenLineEdit->setInputMask("00000");
    portListenLineEdit->setAlignment(Qt::AlignRight);
    //portLineEdit->setReadOnly(true);
    //portListenLineEdit->setEnabled(false);
    connect(portListenLineEdit,SIGNAL(editingFinished()),
            this,SLOT(updatePort()));

    startBtn = new QPushButton("开始");
    connect(startBtn,&QPushButton::clicked,
            this,&Widget::startBtnClicked);

    stopBtn = new QPushButton("停止");
    connect(stopBtn,&QPushButton::clicked,
            this,&Widget::stopBtnClicked);
    stopBtn->setEnabled(false);

    closeBtn = new QPushButton("关闭");
    connect(closeBtn,&QPushButton::clicked,
            [=](){close();});


    hLayout1->addWidget(roleLabel);
    hLayout1->addWidget(roleBox);
    hLayout2->addWidget(ipLabel);
    hLayout2->addWidget(ipLineEdit);
    hLayout3->addWidget(portHostLabel);
    hLayout3->addWidget(portHostLineEdit);
    hLayout4->addWidget(portListenLabel);
    hLayout4->addWidget(portListenLineEdit);
    hLayout5->addWidget(startBtn);
    hLayout5->addWidget(stopBtn);
    hLayout5->addWidget(closeBtn);

    vLayout1->addLayout(hLayout1);
    vLayout1->addLayout(hLayout2);
    vLayout1->addLayout(hLayout3);
    vLayout1->addLayout(hLayout4);
    vLayout1->addLayout(hLayout5);

    resize(400,300);
    qDebug()<<"initUI";

}

void Widget::startBtnClicked()
{
    udpRecord = new UDPRecord(hostIp,hostPort);
    udpRecord->startAudio();

    udpPlay = new UDPPlay(listenPort);
    udpPlay->startPlay();

    startBtn->setEnabled(false);
    roleBox->setEnabled(false);
    ipLineEdit->setEnabled(false);
    portListenLineEdit->setEnabled(false);
    stopBtn->setEnabled(true);
}

void Widget::stopBtnClicked()
{
    if((udpPlay == nullptr) ||(udpRecord == nullptr)) return;
    udpRecord->stopAudio();
    udpRecord->deleteLater();

    udpPlay->stopPlay();
    udpPlay->deleteLater();

    startBtn->setEnabled(true);
    roleBox->setEnabled(true);
    ipLineEdit->setEnabled(true);
    portListenLineEdit->setEnabled(true);
    stopBtn->setEnabled(false);

}


